//
//  main.m
//  作业5
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "AreaCity.h"
#import "AreaProvince.h"
#import "AreaDistrict.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
       /** 一、
        1.创建1个包含5个字符串元素的数组；
        2.用for循环倒序输出所有元素；
            3.用for...in完成所有字符串元素的拼接(生成一个
                新的字符串)，并输出拼接好的字符串。
//                       */
#if 0
        NSArray *array = @[@"huangbo", @"huangxiaoming", @"chengdaoming", @"malaxiangguo", @"fukediguo"];
        for (int i = 0; i < array.count; i++) {
            NSLog(@"%@",array[array.count - 1 - i]);
        }
        NSString *newStr = [NSString string];
        for (NSString *str in array) {
            newStr = [newStr stringByAppendingString:str];
        }
        NSLog(@"%@",newStr);
        /** 二、
         1.创建1个包含5个person对象的数组(姓名用英文)；
         2.用for循环找到年龄最大的Person对象；
         3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
         */
        Person *per = [[Person alloc] init];
        per.name = @"huangxiaoming";
        per.age = 36;
        per.sex = @"男";
        Person *per1 = [[Person alloc] init];
        per1.name = @"zengzhiwei";
        per1.age = 49;
        per1.sex = @"男";
        Person *per2 = [[Person alloc] init];
        per2.name = @"liyifeng";
        per2.age = 30;
        per2.sex = @"男";
        Person *per3 = [[Person alloc] init];
        per3.name = @"guocaijie";
        per3.age = 78;
        per3.sex = @"女";
        Person *per4 = [[Person alloc] init];
        per4.name = @"jorcechu";
        per4.age = 22;
        per4.sex = @"女";
        Person *per5 = [[Person alloc] init];
        per5.name = @"cangjingkong";
        per5.age = 35;
        per5.sex = @"女";
        NSArray *perArr = @[per,per1,per2,per3,per4,per5];
        NSInteger maxAge = 0;
        NSInteger maxIndex = 0;
        for (int i = 0; i < perArr.count; i++) {
            if (maxAge < [perArr[i] age]) {
                maxAge = [perArr[i] age];
                maxIndex = i;
            }
        }
        NSLog(@"年龄最大是%@",perArr[maxIndex]);

        for (Person *person in perArr) {
            if (person.age > 30) {
                person.name = [person.name stringByAppendingString:@"-brother"];
                  NSLog(@"%@",person);
            }
        }
        /** 三
         1.使用第二题目中生成的数组；
         2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
         3.使用NSSortDescriptor，按姓名进行排序。*/
        NSArray *perArr1 = [perArr sortedArrayUsingSelector:@selector(compareAge:)];
        NSLog(@"按年龄排序后%@",perArr1);
        NSArray *perArr2 = [perArr sortedArrayUsingSelector:@selector(compareName:)];
        NSLog(@"按姓名排序后%@",perArr2);
        NSArray *perArr3 = [perArr sortedArrayUsingSelector:@selector(compareSex:)];
        NSLog(@"按性别排序后%@",perArr3);
        NSSortDescriptor *nameSort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
        perArr = [perArr sortedArrayUsingDescriptors:@[nameSort]];
        NSLog(@"%@",perArr);
#endif
        /** 四、
         处理省市区文件中的area.txt
         将省市区进行分割处理, 省里包含市, 市里包含区
         1. 先用数组字典表示
         2. 将数组字典的处理方式转换成数组模型(Class)*/
        NSString *path = @"/Users/dllo/DLS160710/Objective-C/HomeWork/作业5/作业5/area.txt";
        NSString *dataString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        NSArray *dataArray = [dataString componentsSeparatedByString:@"\n"];
#if 0
        NSMutableArray *country = [NSMutableArray array];
        NSString *pronvince = [NSString string];
        NSString *city = [NSString string];
        for ( NSString *line in dataArray) {
            if (![line hasPrefix:@" "]) {
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                NSMutableArray *arr = [NSMutableArray array];
                [dic setObject:arr forKey:line];
                [country addObject:dic];
                pronvince = line;
            } else if ([line hasPrefix:@"  "] && ![line hasPrefix:@"   "]) {
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                NSMutableArray *arr = [NSMutableArray array];
                [dic setObject:arr forKey:line];
                [[[country lastObject] objectForKey:pronvince] addObject:dic];
                city = line;
            } else {
                [[[[[country lastObject] objectForKey:pronvince] lastObject]objectForKey:city] addObject:line];
            }
        }
        NSLog(@"%@",country);
#endif
        
#if 0
        NSMutableArray *mutableArray = [NSMutableArray array];
        for (NSString *str in dataArray) {
            if (![str hasPrefix:@" "]) {
                NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
                [provinceDic setValue:str forKey:@"name"];
                [mutableArray addObject:provinceDic];
                NSMutableArray *cityArray = [NSMutableArray array];
                [provinceDic setValue:cityArray forKey:@"cityArray"];
            } else if ([str hasPrefix:@"  "] && ![str hasPrefix:@"   "]) {
                NSMutableDictionary *proDic = [mutableArray lastObject];
                NSMutableArray *cityArr = [proDic objectForKey:@"cityArray"];
                NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
                [cityDic setValue:str forKey:@"cityName"];
                [cityArr addObject:cityDic];
                NSMutableArray *areaArray = [NSMutableArray array];
                [cityDic setValue:areaArray forKey:@"areaArray"];
            } else ([str hasPrefix:@"    "]) {
                NSMutableDictionary *proDic = [mutableArray lastObject];
                NSMutableArray *cityArr = [proDic objectForKey:@"cityArray"];
                NSMutableDictionary *cityDic = [cityArr lastObject];
                NSMutableArray *areaArray = [cityDic objectForKey:@"areaArray"];
                [areaArray addObject:str];
            }
        }
        NSLog(@"%@",mutableArray);
#endif
        // 转换成数组模型
#if 0
        NSMutableArray *areaProvinceArr = [NSMutableArray array];
        for (NSString *line in dataArray) {
            if (![line hasPrefix:@" "]) {
                AreaProvince *province = [[AreaProvince alloc] init];
                province.provinceName = line;
                province.cityGroup = [NSMutableArray array];
                [areaProvinceArr addObject:province];
                
            } else if (![line hasPrefix:@"   "]) {
                AreaCity *city = [[AreaCity alloc] init];
                city.cityName = line;
                city.districtGroup = [NSMutableArray array];
                [[areaProvinceArr.lastObject cityGroup] addObject:city];
            } else {
                AreaDistrict *dist = [[AreaDistrict alloc] init];
                dist.districtName = line;
                [[[[areaProvinceArr.lastObject cityGroup] lastObject] districtGroup] addObject:line];
             
            }
        }
        NSLog(@"%@",areaProvinceArr);
#endif
        
#if 0
        NSMutableArray *proArr = [NSMutableArray array];
        for (NSString *ditrict in dataArray) {
            if (![ditrict hasPrefix:@" "]) {
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                [dic setValue:ditrict forKey:@"省名"];
                NSMutableArray *cityArr = [NSMutableArray array];
                [dic setValue:cityArr forKey:@"市组"];
                [proArr addObject:dic];
            } else if (![ditrict hasPrefix:@"   "]) {
                NSMutableArray *arr = [[proArr lastObject] objectForKey:@"市组"];
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                [dic setValue:ditrict forKey:@"市名"];
                [arr addObject:dic];
                NSMutableArray *arr1 = [NSMutableArray array];
                [dic setValue:arr1 forKey:@"区组"];
                
            } else {
                NSMutableDictionary *dic = [proArr lastObject];
                NSMutableArray *arr = [dic objectForKey:@"市组"];
                NSMutableDictionary *dic1 = [arr lastObject];
                NSMutableArray *arr2 = [dic1 objectForKey:@"区组"];
                [arr2 addObject:ditrict];
            }
        }
        NSLog(@"%@",proArr);
#endif
    


    
         /*五、拓展
         上述作业做完, 处理Location1.txt
         要求同第四题。自己寻找规律*/

        NSString *path1 = @"/Users/dllo/DLS160710/Objective-C/HomeWork/作业5/作业5/Location1.txt";
        NSString *dataString1 = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
        NSArray *dataArray1 = [dataString1 componentsSeparatedByString:@","];
        dataArray1 = [dataArray1 sortedArrayUsingComparator:^NSComparisonResult(NSString*  _Nonnull obj1, NSString*  _Nonnull obj2) {
            NSArray *arr1 = [obj1 componentsSeparatedByString:@"|"];
            NSArray *arr2 = [obj2 componentsSeparatedByString:@"|"];
            int aNum = [arr1[1] intValue];
            int bNum = [arr2[1] intValue];
            if (aNum > bNum) {
                return NSOrderedDescending;
            } else if (aNum < bNum) {
                return NSOrderedAscending;
            } else {
                return NSOrderedSame;
            }
        }];
#if 0
        NSMutableArray *country1 = [NSMutableArray array];
        NSString *province;
        NSString *city1;
        for (NSString *line in dataArray1) {
            if ([line hasSuffix:@"0000"]) {
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                NSMutableArray *arr = [NSMutableArray array];
                [dic setObject:arr forKey:line];
                [country1 addObject:dic];
                province = line;
            } else if ([line hasSuffix:@"00"]){
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                NSMutableArray *arr = [NSMutableArray array];
                [dic setObject:arr forKey:line];
                [[[country1 lastObject] objectForKey:province] addObject:dic];
                city1 = line;
            } else {
                [[[[[country1 lastObject] objectForKey:province] lastObject] objectForKey:city1] addObject:line];
            }
        }

        NSLog(@"%@",country1 );
#endif
#if 0
        NSMutableArray *country3 = [NSMutableArray array];
        for (NSString *line in dataArray1) {
            if ([line hasSuffix:@"0000"]) {
                NSMutableDictionary *dic =[NSMutableDictionary dictionary];
                NSMutableArray *arr= [NSMutableArray array];
                [dic setObject:arr forKey:@"市组"];
                [dic setObject:line forKey:@"省名"];
                [country3 addObject:dic];

            } else if ([line hasSuffix:@"00"]) {
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                NSMutableArray *arr = [NSMutableArray array];
                [dic setObject:arr forKey:@"区组"];
                [dic setObject:line forKey:@"市名"];
                NSMutableArray *cityArr = [[country3 lastObject] objectForKey:@"市组"];
                [cityArr addObject:dic];
            } else {
                NSMutableArray *cityArr = [[country3 lastObject] objectForKey:@"市组"];
                NSMutableArray *districArr = [[cityArr lastObject] objectForKey:@"区组"];
                [districArr addObject:line];
            }
            
        }
        NSLog(@"%@",country3);
#endif
#if 0
        // 数组模型
        
        NSMutableArray *country2 = [NSMutableArray array];
        for (NSString *line in dataArray1) {
            if ([line hasSuffix:@"0000"]) {
                AreaProvince *areaPro = [[AreaProvince alloc] init];
                areaPro.provinceName = line;
                areaPro.cityGroup = [NSMutableArray array];
                [country2 addObject:areaPro];
            } else if ([line hasSuffix:@"00"]) {
                AreaCity *city = [[AreaCity alloc] init];
                city.cityName = line;
                city.districtGroup = [NSMutableArray array];
                [[[country2 lastObject] cityGroup] addObject:city];
            } else {
                [[[[[country2 lastObject] cityGroup] lastObject] districtGroup] addObject:line];
            }
        }
        NSLog(@"%@",country2);
#endif
    }
    return 0;
}
